package com.pig4cloud.pig.demo.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.security.annotation.Inner;
import com.pig4cloud.pig.demo.Service.CollectionService;
import com.pig4cloud.pig.demo.Util.TokenUtils;
import com.pig4cloud.pig.demo.feign.Entity.Collection;
import com.pig4cloud.pig.demo.feign.Entity.CollectionVO;
import com.pig4cloud.pig.demo.feign.vo.ArticleCollectionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Inner
public class CollectionController {
    @Autowired
    private CollectionService collectionService;

    @GetMapping("/collection/insertCollection/{aid}/{uid}")
    public R insertCollection(@PathVariable Long aid, @PathVariable Long uid) {
        Collection collection = new Collection();
        collection.setAid(aid);
        collection.setUid(uid);

        int insert = collectionService.insert(collection);
        if (insert > 0)
            return R.ok();
        return R.failed();
    }

    @GetMapping("/collection/findCollectionByUidAndCid/{aid}/{uid}")
    public R findCollectionByUidAndCid(@PathVariable Long aid, @PathVariable Long uid) {
        Collection collection = collectionService.findByUidAndAid(aid, uid);
        if (collection == null)
            return R.failed();
        return R.ok();
    }

    @GetMapping("/collection/updateCollectionById/{aid}/{uid}")
    public R updateCollectionByUidAndCid(@PathVariable Long aid, @PathVariable Long uid){
        CollectionVO collectionVO = collectionService.updateCollectionById(aid, uid);
        if (collectionVO == null)
            return R.failed();
        return R.ok(collectionVO);
    }

    /**
     * 从mysql中分页查询收藏文章
     * @param page
     * @param limit
     * @return
     */
    @Inner(value = false)
    @GetMapping("article/collection/{page}/{limit}")
    public R queryArticleCollectionVO(
            @PathVariable Integer page,
            @PathVariable Integer limit,
            HttpServletRequest request) {
        Page<ArticleCollectionVO> page1 = new Page<>(page, limit);
        Long id = TokenUtils.getId(request);
        return R.ok(collectionService.queryArticleCollectionVO(page1,id));
    }



    /**
     * 通过id删除文章
     * @param id
     * @return
     */
    @Inner(value = false)
    @DeleteMapping("article/collection/{id}")
    public R delete(@PathVariable Integer id){
        //删除mysql中的数据
        collectionService.removeById(id);

        return R.ok();
    }


}
